import { FAction } from '@/plugins/constants.ts'
import {
  createMeeting,
  sendRobotMessage,
  visitorLeave,
} from '@/plugins/session.ts'
import socketServer from '@/plugins/socketServer.ts'
import { useChatStore } from '@/store/chat.ts'
import { useSessionStore } from '@/store/session.ts'
import { remove } from 'lodash'
import { v7 as uuidv7 } from 'uuid'
const sendMessage = async (value: any, callback: () => void) => {
  const session = useSessionStore()
  const chat = useChatStore()
  switch (session.FAction) {
    case FAction.INIT:
    case FAction.ROBOT:
      // 发送机器人会话
      await sendRobotMessage(value, () => {
        chat.updateMessages({
          content: value,
          role: 'visitor',
          type: 'text',
          id: uuidv7(),
        })
        callback && callback()
      })
      break
    case FAction.AGENT:
      // 客服对话
      if (Object.prototype.toString.call(value) === '[object Object]') {
        socketServer.sendMessage(value, value.type, () => {
          callback && callback()
        })
      } else {
        socketServer.sendMessage(
          value,
          '@imCloud_subject_message_user@',
          () => {
            chat.updateMessages({
              role: 'visitor',
              type: 'text',
              state: 'message',
              read: false,
              content: value,
            })
            callback && callback()
          },
        )
      }
      break
    case FAction.REQAGENT:
      // 续排
      socketServer.sendMessage(value, 'queue_msg', () => {
        chat.updateMessages({
          role: 'visitor',
          type: 'text',
          state: 'queue_msg',
          read: false,
          content: value,
        })
        callback && callback()
      })
      break
    case FAction.DESTROY:
      // 此时是要发送机器人消息
      await sendRobotMessage(value, () => {
        chat.updateMessages({
          content: value,
          role: 'visitor',
          type: 'text',
          id: uuidv7(),
        })
        callback && callback()
      })
      break
    case FAction.DESTROY_ROBOT:
    case FAction.CANCEL_QUEUE:
    case FAction.TIMEOUT:
      // 机器人销毁状态
      // 1、初始化机器人
      await createMeeting('reload')
      // 2、发送机器人消息
      await sendRobotMessage(value, () => {
        chat.updateMessages({
          content: value,
          role: 'visitor',
          type: 'text',
          id: uuidv7(),
        })
        callback && callback()
      })
      break
    case FAction.VISITOR_LEAVE:
      // 留言
      const result: any = await visitorLeave('leaveMesg', value)
      console.log('result', result)
      if (result.code === 0) {
        remove(chat.messages, { type: 'leave' })
        // 留言成功
        chat.updateMessages({
          content: value,
          role: 'visitor',
          type: 'text',
          id: uuidv7(),
        })
        chat.updateLeaveMessages({
          role: 'system',
          type: 'leave',
          state: 'leaveSuccess',
          id: uuidv7(),
          uuid: uuidv7(),
        })
      }

      callback && callback()
      break
  }
}
export default sendMessage
